<template>
  <div class="register-container">
    <el-card class="register-card">
      <div class="register-header">
        <h2>创建账户</h2>
      </div>
      <el-form :model="form" ref="form" :rules="rules" label-width="100px" class="register-form">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmPassword">
          <el-input type="password" v-model="form.confirmPassword" placeholder="确认密码" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm" block>注册</el-button>
        </el-form-item>
        <el-form-item class="link-login">
          <span @click="goToLogin">已有账户？登录</span>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import axios from "axios";

export default {
  data() {
    return {
      form: {
        username: '',
        email: '',
        password: '',
        confirmPassword: '',
      },
      rules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱格式', trigger: 'blur' },
        ],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        confirmPassword: [
          { required: true, message: '请确认密码', trigger: 'blur' },
          { validator: this.checkPasswordMatch, trigger: 'blur' },
        ],
      },
    };
  },
  methods: {
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 发送注册请求
          axios
              .post('/user/register', null, {
                params: {
                  name: this.form.username,
                  email: this.form.email,
                  password: this.form.password,
                },
              })
              .then((response) => {
                if (response.data.code === 200) {
                  this.$message.success('注册成功');
                  // 注册成功后跳转到登录页面
                  this.$router.push('/login');
                } else {
                  this.$message.error(response.data.message);
                }
              })
              .catch((error) => {
                this.$message.error('网络错误，请稍后再试');
              });
        }
      });
    },
    checkPasswordMatch(rule, value, callback) {
      if (value !== this.form.password) {
        callback(new Error('两次密码不一致'));
      } else {
        callback();
      }
    },
    goToLogin() {
      this.$router.push('/login');
    },
  },
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background: #f5f5f5;
  animation: fadeIn 1s ease-in;
  background: linear-gradient(45deg, #f3ec78, #af4261); /* 渐变背景 */
}

.register-card {
  width: 400px;
  border-radius: 15px;
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
  background: white;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
  animation: slideInFromTop 1s ease-out;
}

.link-login {
  text-align: center;
  margin-top: 20px;
  cursor: pointer;
  color: #409eff;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@keyframes slideInFromTop {
  from {
    transform: translateY(-50px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}
</style>
